package com.ly.ql.Wms.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.ly.ql.Wms.Mapper.InStockBillMapper;
import com.ly.ql.Wms.utils.TypeSafeUtils;
import lombok.RequiredArgsConstructor;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.lang.reflect.Type;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;

@Component
@RequiredArgsConstructor
public class MyMetaObjectHandler implements MetaObjectHandler {
    private final InStockBillMapper inStockBillMapper;
 
    @Override
    public void insertFill(MetaObject metaObject) {
        setFieldValByName("CREATETIME", LocalDateTime.now(), metaObject);
        DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        setFieldValByName("ManufDate", LocalDateTime.parse("1979-01-08", format), metaObject);
        setFieldValByName("ExpireDate", LocalDateTime.parse("2059-01-08",format), metaObject);
        setFieldValByName("VERID", 0, metaObject);
        Object recvDate = getFieldValByName("RecvDate",metaObject);//入库日期
        if (recvDate==null){
            setFieldValByName("RecvDate",  LocalDateTime.now(), metaObject);
        }
        Object no = getFieldValByName("No", metaObject);
        if (no==null){
            String FormId = getFieldValByName("FormId", metaObject).toString();
            if (FormId!=null&&FormId.length()>0){
                setFieldValByName("No",TypeSafeUtils.getNo(FormId),metaObject);
           }
        }
    }
    @Override
    public void updateFill(MetaObject metaObject) {
        // 更新时不设置默认日期，如果需要可以在这里实现
        Object recvDate = getFieldValByName("RecvDate",metaObject);//入库日期
        DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        if (recvDate==null){
            setFieldValByName("RecvDate", LocalDateTime.parse("2059-01-08",format), metaObject);
        }
    }
}